package com.bijava.controller;

import com.bijava.model.pojo.User;
import com.bijava.model.vo.LoginVO;
import com.bijava.model.vo.UserVO;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bijava
 * @version 1.0
 * @since 2020-03-11
 */
@Api(tags = "用户管理接口类", description = "用户的增删改查等接口")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "请求已完成"),
        @ApiResponse(code = 201, message = "资源成功被创建"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 403, message = "服务器接受请求，但是拒绝处理"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器出现异常")
})
@RestController
public class UserController {
    @ApiOperation(value = "用户登录",notes = "用户登录接口详细描述")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginVO", value = "登录参数", paramType = "body", dataType = "LoginVO", required = true)
    })
    @PostMapping("/user/login")
    public User login(@RequestBody @Validated LoginVO loginVO,
                                    BindingResult result, HttpServletRequest request) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        return new User(1l,"张三",1,"广州市天河区",13926561852L);
    }


    @ApiOperation(value = "分页查询所有用户", notes = "分页查询所有用户接口描述", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "path", required = true),
            @ApiImplicitParam(name = "limit", value = "每页大小", dataType = "int", paramType = "path", required = true)
    })
    @GetMapping("/user/{page}/{limit}")
    public List<User> selectUser(@PathVariable("page") int page,
                                 @PathVariable("limit") int limit, UserVO userVO){
        User user = new User(1l,"张三",1,"广州市天河区",13926561852L);
        List<User> userList = new ArrayList<>();
        userList.add(user);
        return userList;
    }
}
